package week9;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class 课堂训练1 {
    public static void main(String[] args) throws IOException {

//        1.从键盘可以输入任意个整数（int）；
        Scanner scanner = new Scanner(System.in);

        System.out.println("键入任意个整数，输入非整数时停止该操作并将所键入数存储：");
        List elements = new LinkedList();
        while(scanner.hasNextInt()){
            int element = scanner.nextInt();
                elements.add(element);
                scanner.hasNext();
            }


        Integer[] intArray1 = (Integer[]) elements.toArray(new Integer[0]);
        System.out.print("键入的数为：");
        for (Integer output1 : intArray1)
        {
           System.out.print(output1 + "\t");
        }


        try {
//            2.写入文件；
            File file = new File("D:\\huawei\\Javawindows文件", "sort.txt");
            if (!file.exists())
            {
                file.createNewFile();
            }
            Writer writer = new FileWriter(file);
            for (Integer output : intArray1)
            {
                String a = output + " ";
                writer.write(a);
            }
            String b = "\r\n";
            writer.write(b);
            writer.flush();

//            3.从文件读取所有的整数；
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line = "";
            System.out.print("\r\n从文件中读取的内容为：");
            List nums = new LinkedList();
            while (line != null) {
                line = bufferedReader.readLine();
                if(line==null)
                    break;
                else {

//                    对字符串中的空格进行处理
                    String s[] = line.trim().split(" ");
                    for (int i = 0; i < s.length; i++){
                    int num = Integer.valueOf(s[i]);
                    nums.add(num);
                    System.out.print(s[i] + " ");}
                }
            }
            Integer[] intArray2 = (Integer[]) nums.toArray(new Integer[0]);

//            4.使用插入排序对读取的整数进行排序，并将排序的结果写入原文件。
            selectionSort(intArray2);
            System.out.print("\r\n排序之后：");
            for (Integer output2 : intArray2)
            {
                String c = output2 + " ";
                writer.write(c);
                System.out.print(output2 + " ");
            }
            writer.flush();
        }

//        5.使用try-catch语句处理IO异常，例如文件创建异常
        catch (IOException Exception)
        {
            System.out.println("错误，指定路径不存在");
        }
    }

//    选择排序法
    public static void selectionSort(Comparable[] list)
    {
        int min;
        Comparable temp;

        for (int index = 0; index < list.length-1; index++)
        {
            min = index;
            for (int scan = index+1; scan < list.length; scan++)
                if (list[scan].compareTo(list[min]) < 0)
                    min = scan;

            // Swap the values
            temp = list[min];
            list[min] = list[index];
            list[index] = temp;
        }
    }
}
